package com.dodi.spring.jdbc.firstDemo;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class UserServiceImpl implements UserService {

	private JdbcTemplate jdbcTemplate;
	
	//设置数据源
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public void save(User user) {

		jdbcTemplate.update("insert into s_user(name,age,sex) values(?,?,?)",
				new Object[] { user.getName(),user.getAge(),user.getSex()},
				new int[] {java.sql.Types.VARCHAR,java.sql.Types.INTEGER,java.sql.Types.VARCHAR});
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers() {

		List<User> list = jdbcTemplate.query("select * from s_user", new UserRowMapper());
		
		return list;
	}

	@Override
	public Object queryForObjectTest() {

		RowMapper<User> rm = BeanPropertyRowMapper.newInstance(User.class);
		
		return jdbcTemplate.queryForObject("select * from s_user where id = '1'", rm);
	}
	
	
	
}
